/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.swirrel.installer;

import java.awt.Component;
import java.awt.event.FocusListener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.swirrel.annotation.FocusGained;
import org.swirrel.annotation.FocusLost;
import org.swirrel.listener.SwirrelFocusListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class FocusListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {

    public FocusListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(FocusGained.class,
                FocusLost.class),
                Arrays.<Class<?>>asList(Component.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
        for (FocusListener fl : ((Component) obj).getFocusListeners()) {
            if (fl instanceof SwirrelFocusListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, List<MethodCall> methodCalls) throws IllegalAccessException {
        ((Component) field.get(comp)).addFocusListener(
                new SwirrelFocusListener(methodCalls.get(0), methodCalls.get(1)));
    }
}
